package cn.sc.summer.auth.provider;

import cn.sc.summer.auth.provider.base.BaseAuthenticationProvider;
import cn.sc.summer.auth.token.SmsCodeLoginToken;
import cn.sc.summer.auth.util.ValidateCodeUtil;
import com.alibaba.fastjson.JSONObject;
import cn.sc.summer.token.enums.LoginTypeEnum;
import cn.sc.summer.token.po.UserDetailX;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 类名：手机号密码登录密码验证规则
 *
 * @author a-xin
 * @date 2023/11/22 17:07
 */
@Slf4j
@Service
public class SmsCodeAuthenticationProvider extends BaseAuthenticationProvider {

    private static final long serialVersionUID = 2408737018815352466L;

    private static final String PATH = "path";

    /**
     * 正式验证用户信息之前，进行用户数据验证，比如，是否黑名单，是否限制登录等
     *
     * @param authentication 验证信息
     */
    @Override
    protected void preCheckUser(Authentication authentication) {
    }

    /**
     * 进行用户信息验证
     *
     * @param userDetailX    用户信息
     * @param authentication 登录凭证
     */
    @Override
    protected void postCheckUser(UserDetailX userDetailX, Authentication authentication) {
        String checkJson = String.valueOf(authentication.getCredentials());
        Map<?, ?> checkInfo = JSONObject.parseObject(checkJson, Map.class);

        ValidateCodeUtil.checkSmsCode(checkInfo.get(PATH).toString(),
                checkInfo.get(ValidateCodeUtil.SMS_CODE).toString(),
                checkInfo.get(ValidateCodeUtil.SMS_RANDOM_KEY).toString());
    }

    /**
     * 用户信息验证后置执行
     *
     * @param userDetailX    用户信息
     * @param authentication 登录凭证
     */
    @Override
    protected void afterCheckUser(UserDetailX userDetailX, Authentication authentication) {
    }

    /**
     * 判断是上面 authenticate 方法的 authentication 参数，是哪种类型
     *
     * @param authentication 验证方式
     * @return 是否支持
     */
    @Override
    public boolean supports(Class<?> authentication) {
        return (SmsCodeLoginToken.class.isAssignableFrom(authentication));
    }

    /**
     * Determine the types of data sources supported by this class
     *
     * @return Supported types
     */
    @Override
    public String supports() {
        return LoginTypeEnum.SMS_WEB.getSlogan();
    }

}

